package com.strato.passport.server.v1_0_0.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.strato.base.osgi.v1_0_0.BundleUtil;
import com.strato.passport.server.v1_0_0.activator.Activator;
import com.strato.passport.server.v1_0_0.service.LoginCheckResult;
import com.strato.passport.server.v1_0_0.service.PassportConfig;
import com.strato.passport.server.v1_0_0.service.PassportService;

public class Helper {

	public static Helper instance=null;
	
	public synchronized static Helper getInstance(){
		if(instance==null){
			instance=new Helper();
		}
		return instance;
	}
	
	public PassportConfig UNMODIFIABLE_CONFIG=new PassportConfig(){

		@Override
		public void setRedirecturlParameterName(String redirecturlParameterName) {
			throw new UnsupportedOperationException("IF you want to change the value,you should hold your own PassprotConfig instance in the PassportService");
		}

		@Override
		public void setPopupParameterName(String popupParameterName) {
			throw new UnsupportedOperationException("IF you want to change the value,you should hold your own PassprotConfig instance in the PassportService");
		}

		@Override
		public void setTicketParameterName(String ticketParameterName) {
			throw new UnsupportedOperationException("IF you want to change the value,you should hold your own PassprotConfig instance in the PassportService");
		}

		@Override
		public void setLoginStatusAttributName(String loginStatusAttributName) {
			throw new UnsupportedOperationException("IF you want to change the value,you should hold your own PassprotConfig instance in the PassportService");
		}

		@Override
		public void setUserAttributeInSession(String userAttributeInSession) {
			throw new UnsupportedOperationException("IF you want to change the value,you should hold your own PassprotConfig instance in the PassportService");
		}
		
	};
	
	public PassportService getPassportService() {
		return BundleUtil.getCachedService(Activator.getContext(), PassportService.class, null);
	}
	
	public PassportConfig getPassportConfig(){
		PassportService service = getPassportService();
		PassportConfig config = service.getConfig();
		if(config!=null){
			return config;
		}
		return UNMODIFIABLE_CONFIG;
	}

	public void onLoginSuccess(Object user, HttpServletRequest req, HttpServletResponse resp) {
		storeinSession(user,req,resp);
		getPassportService().storeInCookie(user,req,resp);
		getPassportService().logLoginHistory(user,req,resp);
	}

	public Object extractFromSession(HttpServletRequest req, HttpServletResponse resp) {
		return req.getSession().getAttribute(getPassportConfig().getUserAttributeInSession());
	}

	public void storeinSession(Object user, HttpServletRequest req, HttpServletResponse resp){
		req.getSession().setAttribute(getPassportConfig().getUserAttributeInSession(), user);
	}

	public void doRedirectOnLogin(Object user, HttpServletRequest req, HttpServletResponse resp) throws IOException {
		PassportService service = getPassportService();
		String ticket=service.renewTicket(user, req, resp);
		String u=req.getParameter(getPassportConfig().getRedirecturlParameterName());
		if(u==null||"".equals(u)){
			service.defaultRedirectOnLogin(user,req,resp);
			return;
		}
		StringBuffer url=new StringBuffer(u);
		if(u.indexOf("?")==-1){
			url.append("?");
		}else{
			url.append("&");
		}
		url.append(getPassportConfig().getTicketParameterName()).append("=").append(ticket);
		String popupParam=getPopupParameterName();
		String popup=req.getParameter(popupParam);
		if(popup!=null&&!"".equals(popup)){
			url.append("&").append(popupParam).append("=").append(popup);
		}
		resp.sendRedirect(url.toString());
		return;
	}

	public String getRedirecturlParameterName() {
		return getPassportConfig().getRedirecturlParameterName();
	}

	public String getPopupParameterName() {
		return getPassportConfig().getPopupParameterName();
	}

	public String getTicketParameterName() {
		return getPassportConfig().getTicketParameterName();
	}

	public String getLoginResultAttributName() {
		return getPassportConfig().getLoginStatusAttributName();
	}

	public void forwardLogin(HttpServletRequest req, HttpServletResponse resp) {
		getPassportService().forwardLogin(req,resp);
	}

	public Object extractFromRequest(HttpServletRequest req, HttpServletResponse resp) {
		return getPassportService().extractFromRequest(req,resp);
	}

	public LoginCheckResult check(Object user) {
		return getPassportService().check(user);
	}

	public Object extractFromCookie(HttpServletRequest req, HttpServletResponse resp) {
		return getPassportService().extractFromCookie(req,resp);
	}

	public void clearCookie(HttpServletRequest req, HttpServletResponse resp) {
		getPassportService().removeFromCookie(req,resp);
	}

	public void forwardLogout(HttpServletRequest req, HttpServletResponse resp) {
		getPassportService().forwardLogout(req,resp);
	}

	public String validateTicket(String ticket) {
		return getPassportService().validateTicket(ticket);
	}

	public String getJsonpCallbackParameterName() {
		return getPassportConfig().getJsonpCallbackParameterName();
	}

	public Object desensitize(Object user) {
		return getPassportService().desensitize(user);
	}


}
